﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.ConfigHelper
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;
using System.Configuration;

#nullable enable
namespace Commom.Util;

public static class ConfigHelper
{
  public static string Get(string key)
  {
    return !string.IsNullOrEmpty(key) ? ConfigurationManager.AppSettings[key] : "";
  }

  public static string GetAppSettingValue(string key) => ConfigHelper.GetAppSettingValue(key, true);

  public static string GetAppSettingValue(string key, bool throwException)
  {
    try
    {
      return ConfigurationManager.AppSettings[key];
    }
    catch
    {
      if (throwException)
        throw new Exception($"没有在配置文件里找到名为'{key}'的配置信息。");
      return string.Empty;
    }
  }

  public static bool SetAppSettingValue(string key, string value, bool throwException)
  {
    try
    {
      string appSetting = ConfigurationManager.AppSettings[key];
      ConfigurationManager.AppSettings[key] = value;
      return true;
    }
    catch
    {
      if (throwException)
        throw new Exception($"没有在配置文件里找到名为'{key}'的配置信息。");
      return false;
    }
  }

  public static bool GetAppSettingBooleanValue(string key)
  {
    return ConfigHelper.GetAppSettingBooleanValue(key, true);
  }

  public static bool GetAppSettingBooleanValue(string key, bool throwException)
  {
    try
    {
      string lower = ConfigHelper.GetAppSettingValue(key).ToLower();
      return lower == "y" || lower == "1" || lower == "true";
    }
    catch
    {
      if (throwException)
        throw new Exception($"没有在配置文件里找到名为'{key}'的配置信息。");
      return false;
    }
  }

  public static string GetConnectionString(string key)
  {
    try
    {
      return ConfigurationManager.ConnectionStrings[key].ConnectionString;
    }
    catch (Exception ex)
    {
      throw new Exception($"{ex.Message}|||{ex.StackTrace}");
    }
  }

  public static string DefaultConnectionString
  {
    get => ConfigurationManager.ConnectionStrings["PersonalConnectionString"].ConnectionString;
  }
}
